home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / me310.zip / UE310C.ZIP / LOCK.C < prev    next >
C/C++ Source or Header  |  1988-10-02  |  4KB  |  173 lines

  1. /*    LOCK:    File locking command routines for MicroEMACS
  2.         written by Daniel Lawrence
  3.                                 */
  4.  
  5. #include <stdio.h>
  6. #include "estruct.h"
  7. #include    "etype.h"
  8. #include "edef.h"
  9. #include    "elang.h"
  10.  
  11. #if    FILOCK
  12. #if    BSD | WMCS
  13. #include <sys/errno.h>
  14.  
  15. extern int sys_nerr;        /* number of system error messages defined */
  16. extern char *sys_errlist[];    /* list of message texts */
  17. extern int errno;        /* current error */
  18.  
  19. char *lname[NLOCKS];    /* names of all locked files */
  20. int numlocks;        /* # of current locks active */
  21.  
  22. /* lockchk:    check a file for locking and add it to the list */
  23.  
  24. lockchk(fname)
  25.  
  26. char *fname;    /* file to check for a lock */
  27.  
  28. {
  29.     register int i;        /* loop indexes */
  30.     register int status;    /* return status */
  31.     char *undolock();
  32.  
  33.     /* check to see if that file is already locked here */
  34.     if (numlocks > 0)
  35.         for (i=0; i < numlocks; ++i)
  36.             if (strcmp(fname, lname[i]) == 0)
  37.                 return(TRUE);
  38.  
  39.     /* if we have a full locking table, bitch and leave */
  40.     if (numlocks == NLOCKS) {
  41.         mlwrite(TEXT173);
  42. /*                      "LOCK ERROR: Lock table full" */
  43.         return(ABORT);
  44.     }
  45.  
  46.     /* next, try to lock it */
  47.     status = lock(fname);
  48.     if (status == ABORT)    /* file is locked, no override */
  49.         return(ABORT);
  50.     if (status == FALSE)    /* locked, overriden, dont add to table */
  51.         return(TRUE);
  52.  
  53.     /* we have now locked it, add it to our table */
  54.     lname[++numlocks - 1] = (char *)malloc(strlen(fname) + 1);
  55.     if (lname[numlocks - 1] == NULL) {    /* malloc failure */
  56.         undolock(fname);        /* free the lock */
  57.         mlwrite(TEXT174);
  58. /*                      "Cannot lock, out of memory" */
  59.         --numlocks;
  60.         return(ABORT);
  61.     }
  62.  
  63.     /* everthing is cool, add it to the table */
  64.     strcpy(lname[numlocks-1], fname);
  65.     return(TRUE);
  66. }
  67.  
  68. /*    lockrel:    release all the file locks so others may edit */
  69.  
  70. lockrel()
  71.  
  72. {
  73.     register int i;        /* loop index */
  74.     register int status;    /* status of locks */
  75.     register int s;        /* status of one unlock */
  76.  
  77.     status = TRUE;
  78.     if (numlocks > 0)
  79.         for (i=0; i < numlocks; ++i) {
  80.             if ((s = unlock(lname[i])) != TRUE)
  81.                 status = s;
  82.             free(lname[i]);
  83.         }
  84.     numlocks = 0;
  85.     return(status);
  86. }
  87.  
  88. /* lock:    Check and lock a file from access by others
  89.         returns    TRUE = files was not locked and now is
  90.             FALSE = file was locked and overridden
  91.             ABORT = file was locked, abort command
  92. */
  93.  
  94. lock(fname)
  95.  
  96. char *fname;    /* file name to lock */
  97.  
  98. {
  99.     register char *locker;    /* lock error message */
  100.     register int status;    /* return status */
  101.     char msg[NSTRING];    /* message string */
  102.     char *dolock();
  103.  
  104.     /* attempt to lock the file */
  105.     locker = dolock(fname);
  106.     if (locker == NULL)    /* we win */
  107.         return(TRUE);
  108.  
  109.     /* file failed...abort */
  110.     if (strncmp(locker, TEXT175, 4) == 0) {
  111. /*                          "LOCK" */
  112.         lckerror(locker);
  113.         return(ABORT);
  114.     }
  115.  
  116.     /* someone else has it....override? */
  117.     strcpy(msg, TEXT176);
  118. /*                  "File in use by " */
  119.     strcat(msg, locker);
  120.     strcat(msg, TEXT177);
  121. /*                  ", overide?" */
  122.     status = mlyesno(msg);        /* ask them */
  123.     if (status == TRUE)
  124.         return(FALSE);
  125.     else
  126.         return(ABORT);
  127. }
  128.  
  129. /*    unlock:    Unlock a file
  130.         this only warns the user if it fails
  131.                             */
  132.  
  133. unlock(fname)
  134.  
  135. char *fname;    /* file to unlock */
  136.  
  137. {
  138.     register char *locker;    /* undolock return string */
  139.     char *undolock();
  140.  
  141.     /* unclock and return */
  142.     locker = undolock(fname);
  143.     if (locker == NULL)
  144.         return(TRUE);
  145.  
  146.     /* report the error and come back */
  147.     lckerror(locker);
  148.     return(FALSE);
  149. }
  150.  
  151. lckerror(errstr)    /* report a lock error */
  152.  
  153. char *errstr;        /* lock error string to print out */
  154.  
  155. {
  156.     char obuf[NSTRING];    /* output buffer for error message */
  157.  
  158.     strcpy(obuf, errstr);
  159.     strcat(obuf, " - ");
  160.     if (errno < sys_nerr)
  161.         strcat(obuf, sys_errlist[errno]);
  162.     else
  163.         strcat(obuf, TEXT178);
  164. /*                           "[can not get system error message]" */
  165.     mlwrite(obuf);
  166. }
  167. #endif
  168. #else
  169. lckhello()    /* dummy function */
  170. {
  171. }
  172. #endif
  173.